using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 故障日志Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IFaultLogService), ServiceLifetime = LifeTime.Transient)]
    public class FaultLogService : BaseService<FaultLog>, IFaultLogService
    {
        /// <summary>
        /// 查询故障日志列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<FaultLogDto> GetList(FaultLogQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<FaultLog, FaultLogDto>(parm);

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public FaultLog GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加故障日志
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public FaultLog AddFaultLog(FaultLog model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改故障日志
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateFaultLog(FaultLog model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<FaultLog> QueryExp(FaultLogQueryDto parm)
        {
            var predicate = Expressionable.Create<FaultLog>();

            return predicate;
        }

        public async Task InsertInfoAsync(FaultLog log)
        {
            await InsertAsync(log);
        }

        public async Task<FaultLog> QueryInfoByFaultNoAsync(int faultNo)
        {
            var info = await QueryByClauseAsync(qbc => qbc.FaultNo.Equals(faultNo) && qbc.State.Equals(1));
            if (info == null)
            {
                return null;
            }
            return info;
        }
    }
}